package com.cutJar.core;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import com.conf.log.LOG;
import com.conf.log.impl.LogImpl;
import com.cutJar.Exception.ResourceNullException;
import com.cutJar.listener.CutListener;
import com.cutJar.util.CharStream;
import com.cutJar.util.Resource;

/**
 * 
 * <b>对class.txt进行加工生产 rt.txt和charsets.txt<b>
 * @see
 * <pre>
 *  在精简jre中提供精简中的必要参数
 *  
 * 	根据bat得到的class.txt(程序加载相关类文件) 进行筛选符合条件的系信息
 * 
 * 	targetKey 可设置精简的对象（rt.jar charsets.jar）
 * 
 * 自动生成结果文件
 * 		charsets.txt
 * 		rt.txt
 * 
 * </pre>
 * @author 威 
 * <br>2017年10月7日 下午3:51:49 
 *
 */
public class MainMachining extends AbstractMachining{
	protected int charsetsCount = 0;	//符合rt个数
	
	/**
	 * 剪切预备主要的rt和charsets 
	 * @see com.cutJar.core.AbstractMachining#cutPreStart()
	 * @see #doFor(BufferedReader, BufferedWriter, BufferedWriter, BufferedWriter)
	 */
	public void cutPreStart() throws ResourceNullException{
		//如果查找列含有 targetKey 和 __JVM_DefineClass__ 并且开头不是[Opened 则符合
		//将不符合的写入temp.txt，待观察
		BufferedReader br = null;
		BufferedWriter bw = null;
		BufferedWriter tempBw = null;
		BufferedWriter charsetsWrite = null;
		try {
			br = CharStream.getRead(Resource.getFileSystemResource(classTextPath));//目标文件class.txt流
			bw = getTargetStream();
			tempBw = getTempStream();
			charsetsWrite = getCharsetsStream();
			doFor(br, bw, tempBw, charsetsWrite);
		} catch (IOException e) {
			LogImpl.getInstance().log(LOG.ERROR, e.getMessage());
		}finally{
			try{
				CharStream.free(br);
				CharStream.free(bw);
				CharStream.free(tempBw);
				CharStream.free(charsetsWrite);
			}catch(IOException e){LogImpl.getInstance().log(LOG.ERROR, e.getMessage());}
		}
	}
	
	/**
	 * 循环对每一行进行操作
	 * <p>	 
	 * @param br
	 * @param bw
	 * @param tempBw
	 * @param charsetsWrite
	 * @throws IOException
	 * void
	 * @see #mainStratege(BufferedWriter, BufferedWriter, BufferedWriter, String)
	 * @since 1.0
	 */
	private void doFor(BufferedReader br, BufferedWriter bw, BufferedWriter tempBw, BufferedWriter charsetsWrite)
			throws IOException {
		String line;
		while((line = br.readLine()) != null){
			if(!line.trim().equals("")){
				allCount++;
				if(listener != null)
					listener.allPre();
				if(line.indexOf("[Opened") != 0){
					mainStratege(bw, tempBw, charsetsWrite, line);
				}	
			}
		}
	}
	
	/**
	 * 剪切预备主要的rt和charsets 
	 * @see
	 * @param bw
	 * @param tempBw
	 * @param charsetsWrite
	 * @param line
	 * @throws IOException
	 * void
	 */
	private void mainStratege(BufferedWriter bw, BufferedWriter tempBw, BufferedWriter charsetsWrite, String line)
			throws IOException {
		if(line.indexOf(dectoryName+".jar") != -1 || line.indexOf("__JVM_DefineClass__") != -1){
			mainCount++;
			if(listener != null)
				listener.rtPre();
			bw.write(editLineData(line));
			bw.newLine();
		}else if(line.indexOf("charsets.jar") != -1){
			charsetsCount++;
			if(listener != null)
				listener.charsetsPre();
			charsetsWrite.write(editLineData(line));
			charsetsWrite.newLine();
		}else{
			tempBw.write(line);
			tempBw.newLine();
		}
	}

	/*==============================模板方法===============================*/
	protected BufferedWriter getCharsetsStream() throws ResourceNullException {
		return null;
	}
	/*==============================模板方法===============================*/
	
	public void logResultInfo(){
		System.out.println("所有目标个数：" +allCount);
		System.out.println("符合rt目标个数：" +mainCount);
		System.out.println("符合charsets目标个数：" +charsetsCount);
		System.out.println("jre剪切预备完毕。");
		LogImpl.getInstance().log(LOG.ORDINARY, new StringBuilder("\r\n")
				.append("预备剪切结果: ")
				.append("\r\n")
				.append("			所有目标个数：")
				.append(allCount+"个")
				.append("\r\n")
				.append("			符合rt目标个数：")
				.append(mainCount+"个")
				.append("\r\n")
				.append("			符合charsets目标个数：")
				.append(mainCount+"个")
				.append("\r\n")
				.append("jre剪切预备完毕."));
	}
}
